import LanguageMng from "./LanguageMng";
import GameMgrBase from "./GameMgrBase"
import PokerModelTs from "../NodeComs/Prefabs/PokerModelTs";
import LogicConfigTs from "../Utils/LogicConfigTs";

cc.Class({
    extends: GameMgrBase,

    properties: {

        headNodeDown:{
            default:null,
            type:cc.Node,
        },

        pokersNode:{
            default:null,
            type:cc.Node
        },

        pokerModel:{
            default:null,
            type:cc.Prefab
        },

        handPokerParent:{
            default:null,
            type:cc.Node,
        },

        pokerStackGamePos:{
            default:null,
            type:cc.Node
        },

        bottomModel:{
            default:null,
            type:cc.Prefab
        },

        bottomParent:{
            default:null,
            type:cc.Node
        },

        disPokerStack:{
            default:null,
            type:cc.Node
        },

        disCardModel:{
            default:null,
            type:cc.Prefab
        },

        finishSlotNode:{
            default:null,
            type:cc.Node
        },

        guildParent:{
            default:null,
            type:cc.Node
        }
    },

    onLoad () {
        this._initBase()

        cc.gg.emitEvent = this.emitEvent.bind(this)
        cc.gg.registeListener = this.registeListener.bind(this)

        //Server force player to discard a poker
        // cc.gg.registeListener("player_option_discard_passive",this.startDiscardFlowPassive.bind(this),this)
        cc.gg.registeListener("option_player_ready",this._onReadyClicked.bind(this),this)

        cc.gg.registeListener("player_start_grab_from_discards",this._onPlayerStartGrabFromDiscards.bind(this),this)
        cc.gg.registeListener("player_cancel_grab_from_discards",this._onPlayerCancelGrabFromDiscards.bind(this),this)
        cc.gg.registeListener("player_grab_discarded_poker",this._onPlayerGrabDicardedPoker.bind(this),this)

        // cc.gg.registeListener("anim_hand_pokers_fly",this._onHandPokerFly.bind(this),this)
        this.node.on("one_poker_fit_anim_finish",this._onOnePokerFitMoveFinish.bind(this),this)
        this.node.on("game_poker_fit_move_finish",this._onPokerFitMoveFinish.bind(this),this)
        // cc.gg.registeListener("anim_poker_seperated_over",this.onHandPokerSeperateOver.bind(this),this)
        cc.gg.registeListener("update_bottoms_width",this._onUpdateBottomsWidth.bind(this),this)
        cc.gg.registeListener("data_move_poker",this._onMovePokerData.bind(this),this)

        // cc.gg.registeListener("player_drag_poker_end",this._onPlayerDragPokerEnd.bind(this),this)
        cc.gg.registeListener("add_bottom_to_right",this._onAddBottomToRight.bind(this),this)
        cc.gg.registeListener("add_bottom_to_left",this._onAddBottomToLeft.bind(this),this)

        cc.gg.registeListener("option_group",this._onOptionGroup.bind(this),this)
        // this.node.on("game_ws_open",this._onGameReconnected.bind(this),this)
        // this.node.on("game_start_reconnect",this._onStartReconnecting.bind(this),this)
        this.node.on("game_close_guild",this._onGameCloseGuild.bind(this),this)
        this.node.on("user_offline",this._onUserOffLine.bind(this),this)
        this.node.on("user_online",this._onUserOnLine.bind(this),this)
        // this.node.on("user_leave",this._onUserLeave.bind(this),this)
        this.node.on("game_oponnent_grap",this._onRoomOpponentGrap.bind(this),this)
        this.node.on("game_self_grap",this._onSelfGrab.bind(this),this)

        this.node.on("guild_group_567",this._onGuildGroup567.bind(this),this)
        this.node.on("guild_grab_diamond10",this._onGuildDiamond10Clicked.bind(this),this)
        this.node.on("guild_discard_2",this._onGuildDiscard2.bind(this),this)
        this.node.on("guild_group_10q10",this._onGuildGroup10q10.bind(this),this)
        this.node.on("guild_finish_13",this._onGuildFinish13.bind(this),this)

        // this.node.on("data_score_result_clear",this._onDataScoreResultClear.bind(this),this)

        this.deckScale = 0.8
        this.pokerInterval = 50
        this.groupInterval = 6
        this.bottomMarginLR = 6
        this.pokerWidth = this.pokerModel.data.width
        this.soundMgrNode = cc.find("SoundManager",this.node)
        this.playCard = null    //如果不出牌，接下来会被自动打出的那张牌
        this.someOneFinish = false  //只要有人finish了，就是true
        this.deckBlinking = false   //两个摸牌库是否正在闪烁

        this.pokerStackNode.scale = this.deckScale
        this.disPokerStack.scale = this.deckScale
        this.pokerStackGamePos.scale = this.deckScale
        this.finishSlotNode.scale = this.deckScale
    },

    _onPokerFitMoveFinish:function(){
        if(cc.gg.dataMgr.playGuild){
            if(cc.gg.dataMgr.guildData.step === 2){
                this.startGuildSort567()
                cc.gg.dataMgr.guildData.step = 3
            }else if(cc.gg.dataMgr.guildData.step === 3){
                this.startGuildNoticeDiscard2()
                cc.gg.dataMgr.guildData.step = 4
            }else if(cc.gg.dataMgr.guildData.step === 5){
                cc.gg.dataMgr.guildData.step = 6
                this.startGuildGroup2()
            }else if(cc.gg.dataMgr.guildData.step === 6){
                cc.gg.dataMgr.guildData.step = 7
                this.startGuildFinish()
            }
        }
    },

    _onOnePokerFitMoveFinish:function(){
        this._pokerFitMoveCount++
        if(this._pokerFitMoveCount === this._pokerFitMoveMax){
            this.node.emit("game_poker_fit_move_finish")
        }
    },

    _onGuildFinish13:function () {
        cc.gg.dataMgr.pokerCanClick = true
        cc.gg.handPokerMgr.selectedEndPokerNodeArray = []
        let pokerNodes = this.handPokerParent.children
        for(let i=0;i<pokerNodes.length;i++){
            let node = pokerNodes[i]
            let poker = node.poker

            if(poker.poker === 0x2d){
                cc.gg.handPokerMgr.addSelectedPoker(node)
                break
            }
        }

        cc.find("Part1",this.nodeGuildFinish).active = false
        cc.find("Part2",this.nodeGuildFinish).active = true
        this.doFinish()

        let a = LanguageMng.getSentence(3)
        let b = LanguageMng.getSentence(53)
        let c = LanguageMng.getSentence(54)
        let d = LanguageMng.getSentence(144)
        cc.gg.toastMgr.showDialog(a,b,c,d,
            function () {
                cc.director.loadScene(LogicConfigTs.SCENENAME_GAME_2)
            }.bind(this),
            function () {
                cc.gg.dataMgr.playGuild = false
                cc.gg.gameDataUtil.goToLobby(true)
            }.bind(this)
        )
    },

    //用户打2
    _onGuildDiscard2:function () {
        this.node.emit("flow_player_turn",cc.gg.dataMgr.guildData.opponentId,60)
        this.nodeGuildNoticePlay2.destroy()
        let diamond2Node = null
        let pokerNodes = this.handPokerParent.children
        for(let i=0;i<pokerNodes.length;i++){
            let node = pokerNodes[i]
            let poker = node.poker

            if(poker.poker === 0x32){
                diamond2Node = node
            }
        }
        this.startGuildOpponentTurn(diamond2Node)
        cc.gg.emitEvent("player_option_discard",diamond2Node)
    },

    startGuildOpponentTurn:function (diamond2Node) {
        this.node.emit("flow_player_turn",cc.gg.dataMgr.guildData.opponentId,60)

        //对手摸牌，从弃牌堆摸方块2
        this.scheduleOnce(function () {
            cc.gg.emitEvent("flow_grab_player_finish",cc.gg.dataMgr.guildData.opponentId,diamond2Node)
        }.bind(this), 2)

        //对手弃牌，打出方块10
        this.scheduleOnce(function () {
            let d10 = cc.gg.dataMgr.generateNewPoker(0x3a)
            this.node.emit("flow_opponent_discard",cc.gg.dataMgr.guildData.opponentId,d10)
            this.changeOptionState(cc.gg.enumConfig.OptionState.GRAB)
        }.bind(this), 4)
    },

    startGuildSort567:function () {
        cc.loader.loadRes("Guild/Guild3",cc.Prefab,function (err, prefab) {
            let newNode = cc.instantiate(prefab)
            this.guildParent.addChild(newNode)
            this.nodeGuild3 = newNode

            cc.loader.releaseRes("Guild/Guild3")
        }.bind(this))
    },

    startGuildNoticeDiscard2:function () {
        cc.loader.loadRes("Guild/GuildNoticePlay2",cc.Prefab,function (err, prefab) {
            let newNode = cc.instantiate(prefab)
            this.guildParent.addChild(newNode)
            this.nodeGuildNoticePlay2 = newNode

            cc.loader.releaseRes("Guild/GuildNoticePlay2")
        }.bind(this))
    },

    //提示玩家摸弃牌堆里的方块10
    startGuildGrab10:function () {
        // this.node.emit("flow_player_turn",cc.gg.dataMgr.getPlayerGameId(),60)
        cc.loader.loadRes("Guild/Guild4",cc.Prefab,function (err, prefab) {
            let newNode = cc.instantiate(prefab)
            this.guildParent.addChild(newNode)
            this.nodeGuild4 = newNode

            cc.loader.releaseRes("Guild/Guild4")
        }.bind(this))
    },

    startGuildGroup2:function () {
        cc.loader.loadRes("Guild/GuildGroup2",cc.Prefab,function (err, prefab) {
            let newNode = cc.instantiate(prefab)
            this.guildParent.addChild(newNode)
            this.nodeGuildGroup2 = newNode

            cc.loader.releaseRes("Guild/GuildGroup2")
        }.bind(this))
    },

    _onGuildDiamond10Clicked:function () {
        this.nodeGuild4.destroy()

        let clickNode = cc.gg.disCardMgr.stackPokerNodes.pop()
        let worPos = clickNode.parent.convertToWorldSpaceAR(clickNode.position)
        cc.gg.emitEvent("player_start_grab_from_discards",clickNode,worPos)

        let pokerModelTs = clickNode.getComponent("PokerModelTs")
        pokerModelTs.dragFromScale = 1
        pokerModelTs.setDisPokerAttr(false,false)

        cc.gg.emitEvent("player_grab_discarded_poker",cc.gg.dataMgr.playerId)
        pokerModelTs.returnToOriginPos()
        cc.gg.dataMgr.guildData.group2.push(clickNode.poker)
    },

    _onGuildGroup567:function () {
        this.nodeGuild3.destroy()
        cc.gg.handPokerMgr.selectedEndPokerNodeArray = []

        for(let i=0;i<cc.gg.dataMgr.guildData.group1.length;i++){
            let node = this._getPokerNodeByPokerId(cc.gg.dataMgr.guildData.group1[i].id)
            cc.gg.handPokerMgr.addSelectedPoker(node)
        }

        let selectingPokerNodes = cc.gg.handPokerMgr.selectedEndPokerNodeArray
        cc.gg.emitEvent("option_group",selectingPokerNodes)

        this.scheduleOnce(function () {
            for(let i=0;i<selectingPokerNodes.length;i++){
                selectingPokerNodes[i].getComponent("PokerModelTs").cancelSelected()
                selectingPokerNodes.splice(i--,1)
            }
            this.uiMgrNode.emit("ui_show_btn_group",false)
            cc.gg.emitEvent("start_group")
            cc.gg.emitEvent("update_bottoms_width")
        }.bind(this), 0)
    },

    _onGuildGroup10q10:function () {
        this.nodeGuildGroup2.destroy()
        cc.gg.handPokerMgr.selectedEndPokerNodeArray = []

        for(let i=0;i<cc.gg.dataMgr.guildData.group2.length;i++){
            let node = this._getPokerNodeByPokerId(cc.gg.dataMgr.guildData.group2[i].id)
            cc.gg.handPokerMgr.addSelectedPoker(node)
        }

        let selectingPokerNodes = cc.gg.handPokerMgr.selectedEndPokerNodeArray
        cc.gg.emitEvent("option_group",selectingPokerNodes)

        this.scheduleOnce(function () {
            for(let i=0;i<selectingPokerNodes.length;i++){
                selectingPokerNodes[i].getComponent("PokerModelTs").cancelSelected()
                selectingPokerNodes.splice(i--,1)
            }
            this.uiMgrNode.emit("ui_show_btn_group",false)
            cc.gg.emitEvent("start_group")
            cc.gg.emitEvent("update_bottoms_width")
        }.bind(this), 0)
    },

    startGuildFinish:function () {
        cc.loader.loadRes("Guild/GuildFinish1",cc.Prefab,function (err, prefab) {
            let newNode = cc.instantiate(prefab)
            this.guildParent.addChild(newNode)
            this.nodeGuildFinish = newNode
            cc.find("Part1",this.nodeGuildFinish).active = true
            cc.find("Part2",this.nodeGuildFinish).active = false

            cc.loader.releaseRes("Guild/GuildFinish1")
        }.bind(this))
    },
    
    _onGameCloseGuild:function () {
        cc.gg.dataMgr.noNeedToReconnect = true
        cc.gg.gameDataUtil.goToLobby(true)
    },

    _onSelfGrab:function (id, from, pokerObj) {
        if(id !== cc.gg.dataMgr.getPlayerGameId()){
            return
        }

        cc.gg.gameDataUtil.addHandPokerObj(cc.gg.dataMgr.getPlayerUser().cardObjs,pokerObj)

        this.soundMgrNode.getComponent("SoundManager").playKaPaiSound()
        if(from === cc.gg.enumConfig.GrabFrom.DECK){
            cc.logfl("Our Player grab a card from deck")
            this.setPlayCard(pokerObj)
            //get the last bottom's last poker position
            let lastBot = this.pokerBottoms[this.pokerBottoms.length-1]
            lastBot.pokers.push(pokerObj)

            cc.gg.emitEvent("flow_grab_player_finish",id,pokerObj)
        }else if(from === cc.gg.enumConfig.GrabFrom.DISCARDS){
            cc.logfl("You grab a card from discard deck")
            //服务端不向User对象同步这个消息，这个消息会在Room中同步
            this.setPlayCard(pokerObj)
            cc.gg.disCardMgr.updateDiscardStack(cc.gg.dataMgr.room.getDicards(cc.gg.disCardMgr.stackShowCount))
        }
    },

    _onUserOffLine:function (id) {
        this.uiMgrNode.emit("ui_user_off_line",id)
    },

    _onUserOnLine:function (id) {
        this.uiMgrNode.emit("ui_user_on_line",id)
    },

    _stopReconnecting:function () {
        this.unschedule(this.reconnectCb)
        this.uiMgrNode.emit("ui_show_reconnect_button",false)
    },

    start () {
        if(cc.gg.dataMgr.playGuild) {

            this.headNodeDown.getComponent("TPPHead").initTPPHead(cc.gg.dataMgr.getPlayerInfo(),cc.gg.enumConfig.RoomType.POINTS)
            this.opponentSeatParent.children[0].getComponent("SeatOther").initSeat({name:"Rinoa",score:0,coin:1000,img:"face_13.png"})

            this.uiMgrNode.emit("ui_show_btn_close_guild",true)
            cc.find("Canvas/UIManager").emit("ui_show_btn_deals",false)
            cc.find("Canvas/UIManager").emit("ui_update_top_info", "#9527", "Points Rummy", 10, "")


            cc.loader.loadRes("Guild/Guild1",cc.Prefab,function (err, prefab) {
                let newNode = cc.instantiate(prefab)
                this.guildParent.addChild(newNode)

                let btnNode = cc.find("bContinue",newNode)
                cc.gg.baseUtil.addClickEventToNode(btnNode,this.node,LogicConfigTs.SCENENAME_GAME_2,"onGuildButtonClicked")

                this.nodeGuild1 = newNode
                cc.loader.releaseRes("Guild/Guild1")
            }.bind(this))
        }else {
            cc.find("Canvas").emit("msgwatcher_add",function () {
                if(cc.gg.dataMgr.getPlayerUser() && cc.gg.dataMgr.room){
                    this._refreshTableUnderReconnecting()
                    return true
                }
                return false
            }.bind(this))
        }

        this.sendTdData()
    },

    _syncResCardsAndCards:function (resCardObjs, cards) {
        let cardsClone = [].concat(cards)
        let length = 0
        for(let i=0;i<resCardObjs.length;i++){
            let cardObjs = resCardObjs[i]
            for(let j=0;j<cardObjs.length;j++){
                length++
            }
        }

        if(length === cardsClone.length)
            return

        if(length < cardsClone.length){
            for(let i=0;i<resCardObjs.length;i++){
                let cardObjs = resCardObjs[i]
                for(let j=0;j<cardObjs.length;j++){
                    let idxInCards = cardsClone.indexOf(cardObjs[j].poker)
                    if(idxInCards > -1){
                        cardsClone.splice(idxInCards,1)
                    }
                }
            }
            cc.logfl("手牌中未分组的牌：",cardsClone)

            let newObjs = []
            for(let i=0;i<cardsClone.length;i++){
                newObjs.push(cc.gg.dataMgr.convToClientPokerObj(cardsClone[i]))
            }
            resCardObjs.push(newObjs)
        }else {
            cc.error("同步来的牌组比手牌还多")
            return
        }
    },

    emitEvent:function (eventName, eventData1, eventData2,eventData3) {
        this.node.emit(eventName, eventData1, eventData2, eventData3)
    },

    registeListener:function (eventName, cbFunc, target) {
        this.node.on(eventName,cbFunc, target)
    },

    // update (dt) {
    //
    // },

    onGuildButtonClicked:function (event) {
        let btnName = event.target.name
        if(btnName === "bContinue"){
            let handCards = []
            this.pokerGroups = []
            let b1 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x01}
            let b2 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x02}
            let b3 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x03}
            handCards.push(b1)
            handCards.push(b2)
            handCards.push(b3)
            this.pokerGroups.push([b1,b2,b3])

            let h1 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x17}
            let h2 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x18}
            let h3 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x19}
            let h12 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x1c}
            handCards.push(h1)
            handCards.push(h2)
            handCards.push(h3)
            handCards.push(h12)
            this.pokerGroups.push([h1,h2,h3,h12])

            let c1 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x25}
            let c2 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x26}
            let c3 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x2a}
            let c4 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x2c}
            let c5 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x2d}
            handCards.push(c1)
            handCards.push(c2)
            handCards.push(c3)
            handCards.push(c4)
            handCards.push(c5)
            this.pokerGroups.push([c1,c2,c3,c4,c5])

            let d1 = {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x32}
            handCards.push(d1)
            this.pokerGroups.push([d1])

            cc.gg.dataMgr.guildData = {
                cards:handCards,
                joker:{id:cc.gg.dataMgr.generateNewPokerId(),poker:0x0c},
                bot:{id:cc.gg.dataMgr.generateNewPokerId(),poker:0x27},
                discards:[{id:cc.gg.dataMgr.generateNewPokerId(),poker:0x27}],
                opponentId:2,
                step:1,
            }
            cc.gg.dataMgr.playerId = 1
            cc.gg.dataMgr.guildData.group1 = []
            cc.gg.dataMgr.guildData.group1.push(c1)
            cc.gg.dataMgr.guildData.group1.push(c2)
            cc.gg.dataMgr.guildData.group2 = []
            cc.gg.dataMgr.guildData.group2.push(c3)
            cc.gg.dataMgr.guildData.group2.push(c4)


            cc.gg.tpgMgr.onStartGame(handCards,0x0c,0x27)
            this.nodeGuild1.destroy()
        }else if(btnName === "bContinue2"){
            cc.gg.emitEvent("flow_player_turn",cc.gg.dataMgr.getPlayerGameId(),60)
            cc.find("Step1",this.nodeGuild2).active = false
            cc.find("Step2",this.nodeGuild2).active = true
        }else if(btnName === "BtnDeckStep2"){
            let newPokerObj = {id:cc.gg.dataMgr.generateNewPokerId(),poker:39}
            let lastBot = this.pokerBottoms[this.pokerBottoms.length-1]
            lastBot.pokers.push(newPokerObj)
            cc.gg.emitEvent("flow_grab_player_finish",cc.gg.dataMgr.getPlayerGameId(),newPokerObj)
            this.nodeGuild2.destroy()
            cc.gg.dataMgr.guildData.step = 2
            cc.gg.dataMgr.guildData.group1.push(newPokerObj)
        }else if(btnName === "BtnDisStep2"){
            let clickNode = cc.gg.disCardMgr.stackPokerNodes.pop()
            let worPos = clickNode.parent.convertToWorldSpaceAR(clickNode.position)
            let pokerModelTs = clickNode.getComponent("PokerModelTs")
            pokerModelTs.dragFromScale = 1
            pokerModelTs.setDisPokerAttr(false,false)
            cc.gg.emitEvent("player_start_grab_from_discards",clickNode,worPos)

            this.nodeGuild2.destroy()

            cc.gg.emitEvent("player_grab_discarded_poker",cc.gg.dataMgr.playerId)
            pokerModelTs.returnToOriginPos()
            cc.gg.dataMgr.guildData.group1.push(clickNode.poker)

            cc.gg.dataMgr.guildData.step = 2
        }
    },

    _onPlayerCancelGrabFromDiscards:function(pokerNode){
        cc.loggrab("player cancel grab from discards")
        cc.gg.disCardMgr.hideTopPokers()
        pokerNode.parent = cc.gg.disCardMgr.dragableParent
        pokerNode.position = cc.Vec2.ZERO
        // pokerNode.getComponent("PokerModelTs").dragFromPos = cc.Vec2.ZERO
        // pokerNode.getComponent("PokerModelTs").dragOriZIndex = 5
        pokerNode.getComponent("PokerModelTs").setFromData(cc.Vec2.ZERO,5,0.8)
        cc.gg.disCardMgr.returnPokerNode(pokerNode)
        for(let i=0;i<cc.gg.tpgMgr.pokerBottoms.length;i++){
            // if(cc.gg.tpgMgr.pokerBottoms[i].pokers.remove(pokerNode.poker)) break
            if(cc.gg.baseUtil.arrayRemove(cc.gg.tpgMgr.pokerBottoms[i].pokers,pokerNode.poker)) break
        }
        cc.gg.emitEvent("update_bottoms_width")
    },

    _onPlayerStartGrabFromDiscards:function(pokerNode,touchPos){
        cc.gg.disCardMgr.removePokerNodeFromDisStack(pokerNode)
        pokerNode.parent = cc.gg.tpgMgr.handPokerParent
        pokerNode.getComponent("PokerModelTs").setPokerScale(this.deckScale)
        pokerNode.position = pokerNode.parent.convertToNodeSpaceAR(touchPos)
        cc.gg.tpgMgr.pokerBottoms[cc.gg.tpgMgr.pokerBottoms.length - 1].pokers.push(pokerNode.poker)
        cc.gg.emitEvent("update_bottoms_width",pokerNode)
    },

    playerCancelGrab:function(pokerNode){
        let worldPos = pokerNode.parent.convertToWorldSpaceAR(pokerNode.position)
        let nodePos = this.disPokerStack.convertToNodeSpaceAR(worldPos)
        this._animToDiscardStack(nodePos,0.12,pokerNode.poker)

        let pokerScript = pokerNode.getComponent("PokerModelTs")
        pokerScript.inDiscardAnim = true
        pokerNode.destroy()
        //data
        for(let i=0;i<this.pokerGroups.length;i++){
            let pokers = this.pokerGroups[i]
            let idx = pokers.indexOf(pokerNode.poker)
            if(idx > -1 && pokers[idx].id === pokerNode.poker.id){
                pokers.splice(idx,1)
                break
            }
        }

        cc.gg.emitEvent("update_bottoms_width",pokerNode)
    },

    // _onPlayerCancelGrabFromDiscards:function (pokerNode) {
    //     cc.gg.disCardMgr.removePokerNodeFromDisStack(pokerNode)
    //     pokerNode.parent = cc.gg.handPokerMgr
    //     pokerNode.getComponent("PokerModelTs").setPokerScale(this.deckScale)
    //     pokerNode.position = pokerNode.parent.convertToNodeSpaceAR(touchPos)
    //     cc.gg.tpgMgr.pokerBottoms[cc.gg.tpgMgr.pokerBottoms.length - 1].pokers.push(pokerNode.poker)
    //     cc.gg.emitEvent("update_bottoms_width",pokerNode)
    // },

    // _onPlayerClickDeclare:function () {
    //     cc.gg.toastMgrA.showToastOnlyLabel("我方点击了Clare")
    // },

    //overwrite
    onStartGame:function (cards, mixCard, bottomCard) {
        this._super(cards)
        this.uiMgr._onFlowStartGame()
    },

    _onOptionGroup:function (pokerArray) {
        this._onAddBottomToLeftWithNodes(pokerArray)
    },

    // _onOpponentDeclare:function (data) {
    //     let oppName = data.oppName
    //     cc.gg.toastMgrA.showDeclareNotice(oppName+LanguageMng.getSentence(135))
    // },

    //overwrite
    changeOptionState:function (data) {
        this._super(data)
        if(cc.gg.dataMgr.playGuild){
            cc.gg.tpgMgr.canDragHandPokers = false
        }
    },

    _dragFinishFail:function (dragNode) {
        this.uiMgrNode.emit("ui_show_finish_indicator",false,null)
        if(dragNode){
            let script = dragNode.getComponent("PokerModelTs")
            script.stopDraging()
            script.returnToOriginPos()
        }
    },

    doFinish:function (dragNode) {
        if(!cc.gg.dataMgr.playGuild && !cc.gg.handPokerMgr.selectedEndPokerNodeArray.length
            || cc.gg.handPokerMgr.selectedEndPokerNodeArray.length > 1 && !dragNode){
            cc.warn("You have not select any poker! 2")
            this._dragFinishFail(dragNode)
            return
        }

        let finishPoker = cc.gg.handPokerMgr.selectedEndPokerNodeArray[0] || dragNode

        if(!cc.gg.dataMgr.playGuild){
            let cardsArray = cc.gg.gameDataUtil.getCardsArrayFromBottoms(this.pokerBottoms,finishPoker.poker)
            if(!cc.gg.sortUtil.CAN_FINISH(cardsArray,cc.gg.dataMgr.getMixCardNum())){
                cc.gg.toastMgr.showToast(LanguageMng.getSentence(121),1.4)
                this._dragFinishFail(dragNode)
                return
            }
        }

        if(cc.gg.dataMgr.playGuild){
            this.discardFinishPoker(cc.gg.handPokerMgr.selectedEndPokerNodeArray[0])
            this.changeOptionState(cc.gg.enumConfig.OptionState.CALCULATING)
            cc.warn("现在显示教程结束提示，询问是否要再学一遍")
            // cc.gg.emitEvent("start_group")
        }else{
            let pokerArray = this.getCommitArr(this.pokerBottoms,cc.gg.handPokerMgr.selectedEndPokerNodeArray[0].poker)
            cc.log("准备提交牌组，客户端现在牌组：",pokerArray)

            // let testArr = [
            //     [14,2,3,4],
            //     [4,5,6],
            //     [7,8,9],
            //     [10,11,12]
            // ]
            cc.log("finish：",pokerArray)
            cc.gg.dataMgr.getPlayerUser().finish(pokerArray,cc.gg.handPokerMgr.selectedEndPokerNodeArray[0].poker.poker)
        }
    },

    discardFinishPoker:function (pokerNode) {
        let worldPos = pokerNode.parent.convertToWorldSpaceAR(pokerNode.position)
        let nodePos = this.finishSlotNode.convertToNodeSpaceAR(worldPos)

        let newDisCard = cc.instantiate(this.disCardModel)
        newDisCard.position = nodePos
        newDisCard.getComponent("DiscardPokerModel").initDiscardPoker(pokerNode.poker)
        this.finishSlotNode.addChild(newDisCard)
        let moveTo = cc.moveTo(0.1,cc.Vec2.ZERO)
        let callFunc = cc.callFunc(function () {
            cc.gg.emitEvent("data_finish_one",pokerNode.poker)
        }.bind(this),this)
        newDisCard.runAction(cc.sequence(moveTo,callFunc,cc.removeSelf()))

        pokerNode.destroy()

        //data
        for(let i=0;i<this.pokerGroups.length;i++){
            let pokers = this.pokerGroups[i]
            let idx = pokers.indexOf(pokerNode.poker)
            if(idx > -1 && pokers[idx].id === pokerNode.poker.id){
                pokers.splice(idx,1)
                break
            }
        }

        cc.gg.emitEvent("update_bottoms_width",pokerNode)
    },

    _onMovePokerData:function (fromBotObj, toBotObj, pokerObj) {
        if(!cc.gg.baseUtil.arrayRemove(fromBotObj.pokers,pokerObj)){
            cc.error("移除失败！")
            return
        }
        toBotObj.pokers.push(pokerObj)
        // cc.log("移动",pokerObj)
        // cc.log(JSON.stringify(fromBotObj.pokers))
        // cc.log(JSON.stringify(toBotObj.pokers))
    },

    _onMovePokerDataSimple:function (toBotObj, pokerObj) {
        let success = false
        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            // success = botObj.pokers.remove(pokerObj)
            success = cc.gg.baseUtil.arrayRemove(botObj.pokers,pokerObj)
            if(success){
                break
            }
        }

        if(!success){
            cc.error("remove poker failed!",pokerObj)
            return
        }

        toBotObj.pokers.push(pokerObj)
    },

    _onAddBottomToRight:function (fromBotObj, pokerNode, bottomIdx) {
        let pokerObj = pokerNode.poker

        let newGroup = []
        this.pokerGroups.push(newGroup)

        let typeObj = this.getGroupType(newGroup,cc.gg.dataMgr.getMixCardNum())
        let newBotObj = {
            width:this.pokerWidth + this.bottomMarginLR*2,
            type:typeObj.type,
            isGreen:typeObj.pure,
            pokers:newGroup,
            pos:null,
            node:null,
        }

        if(this.pokerBottoms[bottomIdx]){
            this.pokerBottoms.splice(bottomIdx,0)
        }else
            this.pokerBottoms[bottomIdx] = newBotObj

        this._onMovePokerData(fromBotObj,newBotObj,pokerObj)

        this._updateBottomNodePosition()

        let botPos = newBotObj.pos

        let newBottom = cc.instantiate(this.bottomModel)
        newBottom.position = botPos
        newBottom.botObj = newBotObj
        this.bottomParent.addChild(newBottom)

        newBotObj.node = newBottom

        cc.gg.emitEvent("update_bottoms_width",pokerNode)
        // this.scheduleOnce(function () {
        //     cc.gg.emitEvent("update_bottoms_width",pokerNode)
        // }, 0)
    },


    _onAddBottomToLeftWithNodes:function (pokerNodes) {
        let newGroup = []
        this.pokerGroups.unshift(newGroup)

        let typeObj = this.getGroupType(newGroup,cc.gg.dataMgr.getMixCardNum())
        let newBotObj = {
            width:(1-1)*this.pokerInterval + this.pokerWidth + this.bottomMarginLR*2,
            type:typeObj,
            isGreen:typeObj.pure,
            pokers:newGroup,
            pos:null,
            node:null,
        }

        this.pokerBottoms.unshift(newBotObj)

        for(let i=0;i<pokerNodes.length;i++){
            this._onMovePokerDataSimple(newBotObj,pokerNodes[i].poker)
        }

        this._updateBottomNodePosition()

        let newBottom = cc.instantiate(this.bottomModel)
        newBottom.position = newBotObj.pos
        newBottom.botObj = newBotObj
        newBotObj.node = newBottom
        this.bottomParent.addChild(newBottom)

        cc.gg.emitEvent("update_bottoms_width")
        // this.scheduleOnce(function () {
        //     cc.gg.emitEvent("update_bottoms_width",pokerNode)
        // }, 0)
    },

    _onAddBottomToLeft:function (dragNode) {
        let newGroup = []
        this.pokerGroups.unshift(newGroup)

        let typeObj = this.getGroupType(newGroup,cc.gg.dataMgr.getMixCardNum())
        let newBotObj = {
            width:(1-1)*this.pokerInterval + this.pokerWidth + this.bottomMarginLR*2,
            type:typeObj,
            isGreen:typeObj.pure,
            pokers:newGroup,
            pos:null,
            node:null,
        }

        this.pokerBottoms.unshift(newBotObj)

        this._onMovePokerDataSimple(newBotObj,dragNode.poker)

        this._updateBottomNodePosition()

        let newBottom = cc.instantiate(this.bottomModel)
        newBottom.position = newBotObj.pos
        newBottom.botObj = newBotObj
        newBotObj.node = newBottom
        this.bottomParent.addChild(newBottom)

        cc.gg.emitEvent("update_bottoms_width",dragNode)
        // this.scheduleOnce(function () {
        //     cc.gg.emitEvent("update_bottoms_width",pokerNode)
        // }, 0)
    },

    _updateBottomType:function () {

        let hasPureSeq = false

        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            let group = botObj.pokers
            botObj.type = this.getGroupType(group,cc.gg.dataMgr.getMixCardNum())
            botObj.isGreen = botObj.type.pure

            if(!hasPureSeq && botObj.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE && botObj.type.pure) hasPureSeq = true
        }

        let seqCount = 0
        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            if(hasPureSeq && botObj.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE){
                botObj.isGreen = true
                seqCount++
            }
        }

        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            if(seqCount > 1 && (botObj.type.type === cc.gg.enumConfig.SeperateType.SET || botObj.type.type === cc.gg.enumConfig.SeperateType.ALL_MIX)){
                botObj.isGreen = true
            }
        }
    },

    _getBottomNodeByBotObj:function (botObj) {
        for(let i=0;i<this.bottomParent.children.length;i++){
            let tmpNode = this.bottomParent.children[i]
            if(tmpNode.botObj === botObj){
                return tmpNode
            }
        }
        return null
    },

    _onUpdateBottomsWidth:function (exceptPokerNode) {
        if(this.gameState === cc.gg.enumConfig.OptionState.GROUP){
            let handCards = this.getCommitArr(this.pokerBottoms,null)
            cc.gg.dataMgr.getPlayerUser().sync(handCards)
        }

        cc.gg.handPokerMgr.clearSelectedNodes()
        this.clearBottoms()
        this._updateBottomType()
        cc.gg.emitEvent("update_bottoms_scores",cc.gg.dataMgr.getPlayerGameId())

        let botObjIdx = 0
        for(let i=0;i<this.pokerGroups.length;i++){
            let group = this.pokerGroups[i]
            if(!group.length)
                continue

            this.pokerBottoms[botObjIdx].width = (group.length-1)*this.pokerInterval + this.pokerWidth + this.bottomMarginLR*2
            // cc.log("一共有",group.length,"个元素，宽度为：",this.pokerBottoms[botObjIdx].width)
            botObjIdx++
        }

        this._updateBottomNodePosition()
        this._updateBottomPokersPosition(exceptPokerNode)
    },

    _updateBottomPokersPosition:function (exceptPokerNode) {
        for(let i=0;i<this.pokerGroups.length;i++){
            let group = this.pokerGroups[i]
            if(!group.length)
                continue

            let botObj = this._getPokerBottomObj(group[0])
            let botPos = botObj.pos
            let botWidth = botObj.width

            let botWorldPos = this.bottomParent.convertToWorldSpaceAR(botPos)
            let nodePos = this.handPokerParent.convertToNodeSpaceAR(botWorldPos)

            //a group of pokers fly to a bottom
            this._pokerFitMoveCount = 0
            this._pokerFitMoveMax = 0
            for(let j=0;j<group.length;j++){
                let pokerId = group[j]
                let pokerNode = this._getPokerNodeByPokerId(pokerId.id)
                pokerNode.getComponent("PokerModelTs").cancelSelected()

                let x = nodePos.x - botWidth/2 + this.bottomMarginLR + this.pokerWidth/2 + j * this.pokerInterval
                let y = nodePos.y + this.pokerModel.data.height/2
                let tarPos = cc.v2(x,y)

                if(pokerNode === exceptPokerNode){
                    exceptPokerNode.getComponent("PokerModelTs").setFromData(tarPos,j,1)
                    // exceptPokerNode.getComponent("PokerModelTs").dragFromPos = tarPos
                    // exceptPokerNode.getComponent("PokerModelTs").dragOriZIndex = j
                    continue
                }

                pokerNode.stopAllActions()
                let moveTo = cc.moveTo(0.1,tarPos)
                let calllfunc = cc.callFunc(function(){
                    let pokerModel = pokerNode.getComponent("PokerModelTs")
                    pokerModel.inHandStatic = true
                    cc.find("Canvas").emit("one_poker_fit_anim_finish")
                }.bind(this),this)
                let seq = cc.sequence(moveTo,calllfunc)
                pokerNode.runAction(seq)
                this._pokerFitMoveMax++

                pokerNode.zIndex = j
            }
        }
    },

    _updateBottomPokersPositionWithChangeIdex:function (inBotObj,newBotPokers,exceptPokerNode) {

        let botPos = inBotObj.pos
        let botWidth = inBotObj.width

        let botWorldPos = this.bottomParent.convertToWorldSpaceAR(botPos)
        let nodePos = this.handPokerParent.convertToNodeSpaceAR(botWorldPos)

        //a group of pokers fly to a bottom
        let hasDoChanged = false
        for(let j=0;j<newBotPokers.length;j++){
            let pokerId = newBotPokers[j]
            let pokerNode = this._getPokerNodeByPokerId(pokerId.id)
            pokerNode.getComponent("PokerModelTs").cancelSelected()

            let x = nodePos.x - botWidth/2 + this.bottomMarginLR + this.pokerWidth/2 + j * this.pokerInterval
            let y = nodePos.y + this.pokerModel.data.height/2
            let tarPos = cc.v2(x,y)

            if(pokerNode === exceptPokerNode){
                exceptPokerNode.getComponent("PokerModelTs").setFromData(tarPos,j,1)
                // exceptPokerNode.getComponent("PokerModelTs").dragFromPos = tarPos
                // exceptPokerNode.getComponent("PokerModelTs").dragOriZIndex = j
                continue
            }

            pokerNode.stopAllActions()

            if(!hasDoChanged){
                hasDoChanged = true
                let moveTo = cc.moveTo(0.1,tarPos)
                let doChangeIndex = cc.callFunc(function () {
                    for(let a=0;a<inBotObj.pokers.length;a++){
                        inBotObj.pokers.splice(a--,1)
                    }
                    for(let a=0;a<newBotPokers.length;a++){
                        inBotObj.pokers.push(newBotPokers[a])
                    }
                }.bind(this),this)
                pokerNode.runAction(cc.sequence(moveTo,doChangeIndex))
            }else {
                let moveTo = cc.moveTo(0.1,tarPos)
                pokerNode.runAction(moveTo)
            }

            pokerNode.zIndex = j
        }
    },

    _updateBottomNodePosition:function () {
        let totalLength = (this.pokerBottoms.length - 1)*this.groupInterval
        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            let width = botObj.width
            totalLength += width
        }

        let nowLeftX = - totalLength/2
        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            let width = botObj.width

            nowLeftX += width/2
            let botPos = cc.v2(nowLeftX,0)
            botObj.pos = botPos

            nowLeftX += width/2 + this.groupInterval
        }


        for(let i=0;i<this.bottomParent.children.length;i++){
            let botNode = this.bottomParent.children[i]
            let botObj = botNode.botObj
            botNode.position = botObj.pos

            let botScript = botNode.getComponent("SeperateBottom")
            botScript.showBottom(botObj)
        }
    },

    _onHandPokerSeperate:function () {
        let mixCard = cc.gg.dataMgr.getMixCard().poker
        if(!cc.gg.dataMgr.playGuild){
            this.initHandCardBottomsWithMixCard(cc.gg.dataMgr.getPlayerHandCardObjs(),mixCard)
        }
        this.initPokerBottoms()
    },

    _initHandCardBottomWithRestoreArray:function (cardsArray) {

        this.pokerGroups = []
        for(let i=0;i<cardsArray.length;i++){
            let cards = cardsArray[i]

            this.pokerGroups.push(cards)
        }
    },

    initRestorePokerBottoms:function (exceptPokerNode) {
        cc.log("Reset all bottoms by poker Groups")
        this.pokerBottoms = []

        let pokerWidth = this.pokerModel.data.width
        for(let i=0;i<this.pokerGroups.length;i++){
            let group = this.pokerGroups[i]
            if(!group.length)
                continue

            let typeObj = this.getGroupType(group,cc.gg.dataMgr.getMixCardNum())
            this.pokerBottoms.push({
                width:(group.length-1)*this.pokerInterval + pokerWidth + this.bottomMarginLR*2,
                type:typeObj,
                isGreen:typeObj.pure,
                pokers:group,
                pos:null,
                node:null,
            })
            // cc.log("group",group,"的宽度", (group.length-1)*this.pokerInterval + pokerWidth + this.bottomMarginLR*2)
        }

        let totalLength = (this.pokerBottoms.length - 1)*this.groupInterval
        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            let width = botObj.width
            totalLength += width
        }

        let nowLeftX = - totalLength/2
        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            let width = botObj.width

            nowLeftX += width/2
            let botPos = cc.v2(nowLeftX,0)
            botObj.pos = botPos

            nowLeftX += width/2 + this.groupInterval
        }


        for(let i=0;i<this.pokerBottoms.length;i++){
            let botObj = this.pokerBottoms[i]
            let botPos = botObj.pos

            let newBottom = cc.instantiate(this.bottomModel)
            newBottom.position = botPos
            newBottom.botObj = botObj
            this.bottomParent.addChild(newBottom)

            botObj.node = newBottom

            let botScript = newBottom.getComponent("SeperateBottom")
            botScript.showBottom(botObj)
        }
        this.scheduleOnce(function () {
            cc.gg.emitEvent("update_bottoms_scores",cc.gg.dataMgr.getPlayerGameId())
        }, 0)


        for(let a=0;a<this.pokerGroups.length;a++){
            let group = this.pokerGroups[a]
            if(!group.length)
                continue

            let botObj = this._getPokerBottomObj(group[0])
            let botPos = botObj.pos
            let botWidth = botObj.width

            let botWorldPos = this.bottomParent.convertToWorldSpaceAR(botPos)
            let nodePos = this.handPokerParent.convertToNodeSpaceAR(botWorldPos)

            //a group of pokers fly to a bottom
            for(let j=0;j<group.length;j++){
                let pokerId = group[j]
                let pokerNode = this._getPokerNodeByPokerId(pokerId.id)
                if(pokerNode === exceptPokerNode)
                    continue

                let x = nodePos.x - botWidth/2 + this.bottomMarginLR + pokerWidth/2 + j * this.pokerInterval
                let y = nodePos.y + this.pokerModel.data.height/2
                let tarPos = cc.v2(x,y)

                let moveTo = cc.moveTo(0.4,tarPos)
                pokerNode.runAction(moveTo)

                pokerNode.zIndex = j
            }
        }
    },

    _onHandPokerFly:function () {
        if(!this._handPokerFlyCountAlready){
            this._handPokerFlyCountAlready = 1
            return
        }

        this._handPokerFlyCountAlready++
        if(this._handPokerFlyCountAlready === this._handPokerFlyCount){

            let allPokeNodes = this.handPokerParent.children
            for(let i=0;i<allPokeNodes.length;i++){
                let pokeNode = allPokeNodes[i]
                let moveToMiddle = cc.moveTo(0.4,cc.Vec2.ZERO)
                pokeNode.runAction(moveToMiddle)
            }

            this.scheduleOnce(this._onHandPokerSeperate.bind(this), 0.4)
        }
    },

    initTable:function () {
        this._initPlayerHandPokersPosition()
    },

    _onPlayerGrabPoker:function (playerId,pokerValue,dragPos) {
        cc.gg.emitEvent("flow_grab_player_finish",playerId,pokerValue,dragPos)
    },

    async _onPlayerGrabDicardedPoker (playerId,dragNode,notDrag) {

        if(cc.gg.dataMgr.playGuild) return

        if(notDrag){
            // dragNode.getComponent("PokerModelTs").setPokerScale(1)
            let disStackPos = this.disPokerStack.parent.convertToWorldSpaceAR(this.disPokerStack.position)
            let localPos = dragNode.parent.convertToNodeSpaceAR(disStackPos)
            dragNode.position = cc.v2(localPos.x + 10,localPos.y + 10)
        }
        var code = await cc.gg.dataMgr.getPlayerUser().grab(cc.gg.enumConfig.GrabFrom.DISCARDS)
        if(code === 0){
            cc.log("【从弃牌堆】抓牌成功")
            cc.gg.emitEvent("update_bottoms_width")
            // dragNode.getComponent("PokerModelTs").returnToOriginPos()
            this.changeOptionState(cc.gg.enumConfig.OptionState.PLAY)
        }else {
            cc.log("【从弃牌堆】抓牌失败！code:",code)
        }
    },

    _onGrabPlayerFinish:function (playerId,pokerObj,dragPos) {
        cc.logfl("anim:Player grab finish")

        let ourPlayerId = cc.gg.dataMgr.getPlayerGameId()

        if(playerId === ourPlayerId){
            cc.gg.tpgMgr.pokerStackDragable = false
            this.pokerStackBlinkStop()

            this.showGrabOnePoker(pokerObj,dragPos)
            cc.logfl("Our player grab card:",pokerObj)
            this.changeOptionState(cc.gg.enumConfig.OptionState.PLAY)   //这里可能有问题
        }else {
            cc.logfl("We don't know what he grabed..")
            this.opponentGrabOnePoker(playerId)
            this.changeOptionState(cc.gg.enumConfig.OptionState.WAIT)
        }
    },

    _initPlayerHandPokersPosition:function () {
        cc.log("Init hand poker positions")
        let downWorldPos = this.handPokerParent.parent.convertToWorldSpaceAR(this.handPokerParent.position)
        let downPos = this.handPokerParent.convertToNodeSpaceAR(downWorldPos)

        let areaWidth = 1000
        let leftPos = cc.v2(downPos.x-areaWidth/2+this.pokerModel.data.width/2,downPos.y)

        this.playerPokerPoses = []
        for(let i=0;i<14;i++){
            this.playerPokerPoses.push(cc.v2(leftPos.x + areaWidth/14 * i,leftPos.y))
        }
    },


    discardPoker:function (pokerNode) {
        this.soundMgrNode.getComponent("SoundManager").playPokerFlySound()
        let worldPos = pokerNode.parent.convertToWorldSpaceAR(pokerNode.position)
        let nodePos = this.disPokerStack.convertToNodeSpaceAR(worldPos)
        this._animToDiscardStack(nodePos,0.12,pokerNode.poker)

        let pokerScript = pokerNode.getComponent("PokerModelTs")
        pokerScript.inDiscardAnim = true
        pokerNode.destroy()
        //data
        for(let i=0;i<this.pokerGroups.length;i++){
            let pokers = this.pokerGroups[i]
            let idx = pokers.indexOf(pokerNode.poker)
            if(idx > -1 && pokers[idx].id === pokerNode.poker.id){
                pokers.splice(idx,1)
                break
            }
        }

        cc.gg.emitEvent("update_bottoms_width",pokerNode)
    },

    async _animToDiscardStack (fromPos,animTime,pokerObj,cb) {
        let newDisCard = cc.instantiate(this.disCardModel)
        newDisCard.position = fromPos

        await newDisCard.getComponent("DiscardPokerModel").initDiscardPoker(pokerObj)

        this.disPokerStack.addChild(newDisCard)

        let moveTo = cc.moveTo(animTime,cc.Vec2.ZERO)
        let callFunc = cc.callFunc(function () {
            if(cc.gg.dataMgr.playGuild){
                cc.gg.dataMgr.guildData.discards.push(pokerObj)
                cc.gg.disCardMgr.updateDiscardStack(cc.gg.dataMgr.guildData.discards)
            }else
                cc.gg.disCardMgr.updateDiscardStack(cc.gg.dataMgr.room.getDicards(cc.gg.disCardMgr.stackShowCount))
            if(cb) cb(pokerObj)
        }.bind(this),this)

        newDisCard.runAction(cc.sequence(moveTo,callFunc,cc.removeSelf()))
    },

    showGrabOnePoker:function (newPokerObj, dragPos) {
        dragPos = dragPos || this.pokerStackNode.parent.convertToWorldSpaceAR(this.pokerStackNode.position)
        let oriPos = this.handPokerParent.convertToNodeSpaceAR(dragPos)

        let newPoker = cc.instantiate(this.pokerModel)
        this.handPokerParent.addChild(newPoker)
        newPoker.position = oriPos
        newPoker.poker = newPokerObj

        let deckWorldY = cc.gg.tpgMgr.pokerStackNode.parent.convertToWorldSpaceAR(cc.gg.tpgMgr.pokerStackNode.position).y
        let handWorldY = cc.gg.tpgMgr.handPokerParent.parent.convertToWorldSpaceAR(cc.gg.tpgMgr.handPokerParent.position).y
        let pokerModelTs = newPoker.getComponent("PokerModelTs")
        pokerModelTs.initPoker(newPokerObj,handWorldY,deckWorldY)
        pokerModelTs.setFromData()

        cc.gg.emitEvent("update_bottoms_width")
        // this.scheduleOnce(function () {
        //     cc.gg.emitEvent("update_bottoms_width")
        // },0)
    },

    restoreOpenDeck:function (discards, mixPoker) {
        let pokerStackPriWorldPos = this.pokerStackNode.parent.convertToWorldSpaceAR(this.pokerStackNode.position)

        //pick bottom discard
        cc.gg.disCardMgr.updateDiscardStack(cc.gg.dataMgr.room.getDicards(cc.gg.disCardMgr.stackShowCount))

        //pick mixcard
        let worldPos2 = cc.gg.baseUtil.getWorldPos(this.pokerStackGamePos)
        let botCardTarPos = this.pokerStackNode.parent.convertToNodeSpaceAR(worldPos2)
        this.pokerStackNode.position = botCardTarPos

        let botCardOriPos = this.pokerStackGamePos.convertToNodeSpaceAR(pokerStackPriWorldPos)
        let newBotCard = cc.instantiate(this.disCardModel)
        newBotCard.position = botCardOriPos
        newBotCard.getComponent("DiscardPokerModel").initDiscardPoker(mixPoker)
        this.pokerStackGamePos.addChild(newBotCard)

        newBotCard.position = cc.v2(-40,0)
        newBotCard.rotation = -90
    },

    onHandPokerSeperateOver:function () {
        if(cc.gg.dataMgr.playGuild){
            this.animOpenDeck(
                {id:cc.gg.dataMgr.generateNewPokerId(),poker:0x27},
                cc.gg.dataMgr.getMixCard()
            )
        }else {
            this._super()
        }
    },

});
